/*
 * Copyright 2008 Wink Saville
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import junit.framework.Assert;

import com.saville.debug.*;
import com.saville.msgcomp.*;
import com.saville.mc.*;
import com.saville.hmc.*;

import com.saville.testhmc.*;

public class TestHmc extends Hmc {

    public static void main(String[] args) {
        IMcMgr mcMgr = McMgr.getInstance();
        Log.appName = "TestHmc";
        Log.print("main()");
        
        /*
         * do the tests
         */
        Test.doThem(0xF, 100000, false);
        
        /*
         * Prepare a message pump so its available
         * when we make TestHmc. Then lookup TestHmc
         * and send it a message. Finally, drop into
         * the message loop.
         */
        MsgPump.prepare();
        
        new TestHmc("TestHmc");
        
        int id = mcMgr.lookup("TestHmc");
        Assert.assertTrue(id != -1);
        
        Msg msg = mcMgr.getMsg(-1, id, 0, McConst.mkCmd(0));
        mcMgr.sendMsg(msg);
        
        MsgPump.loop();
    }
    
    TestHmc(String name) {
        super(name, 1, 0);
        df = Log.VERBOSE;
        Log.d(df, "TestHmc() X");
        
        states[0] = new State_Initial();

        /*
         * C A R E F U L: code after the call to completeConstruction 
         * may be executed after MC_CMD_STARTED is delivered to the
         * initial state.
         */
        completeConstruction();
        Log.d(df, "TestHmc() X", mName);
    }

    private class State_Initial extends State {
        @Override
        public boolean doState(Msg msg) {
            boolean handled;
            
            if (msg.is(McConst.MC_GUID, McConst.MC_CMD_STARTED)) {
                Log.d(df, "Initial STARTED");
                handled = true;
            } else {
                Log.d(df, "Initial got guid=0x%x cmd=0x%x", msg.guid, msg.cmd);
                handled = false;
            }
            
            return handled;
        }
    }
}
